package com.xuecheng.ucenter.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.ucenter.mapper.XcMenuMapper;
import com.xuecheng.ucenter.mapper.XcUserMapper;
import com.xuecheng.ucenter.model.dto.AuthParamsDto;
import com.xuecheng.ucenter.model.dto.XcUserExt;
import com.xuecheng.ucenter.model.po.XcMenu;
import com.xuecheng.ucenter.model.po.XcUser;
import com.xuecheng.ucenter.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static org.bouncycastle.cms.RecipientId.password;


@Slf4j
@Component
public class UserServiceImpl implements UserDetailsService {
    @Autowired
    XcUserMapper xcUserMapper;

    @Autowired
    ApplicationContext applicationContext;


    @Autowired
    XcMenuMapper xcMenuMapper;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //将传入的json转成AuthParamsDto对象
        AuthParamsDto authParamsDto=null;
        try{
             authParamsDto = JSON.parseObject(s, AuthParamsDto.class);
        }catch (Exception e){
            throw  new RuntimeException("请求认证参数不符合要求。");
        }

        //认证类型 策略选择
        String authType = authParamsDto.getAuthType();
        //根据类型从容器中取出不同的bean调用
        String beanName = authType + "_authservice";
        AuthService authService = applicationContext.getBean(beanName,AuthService.class);
        XcUserExt xcUserExt=authService.execute(authParamsDto);
        //封装返回的用户信息为UserDetails
        //Security根据这个生成令牌
        UserDetails userPrincipal = getUserPrincipal(xcUserExt);


        return userPrincipal;
    }

    /**
     * @description 查询用户信息
     * @return com.xuecheng.ucenter.model.po.XcUser 用户信息
     * 封装为需要返回的对象类型
     */
    public UserDetails getUserPrincipal(XcUserExt xcUser){
        String[] authorities={"test"};
        String password = xcUser.getPassword();
        //根据用户id查询用户权限
        List<XcMenu> xcMenus = xcMenuMapper.selectPermissionByUserId(xcUser.getId());
        if(xcMenus.size()>0){
            List<String> permissions=new ArrayList<>();
            xcMenus.forEach(m->{
                permissions.add(m.getCode());
            });
            //将permission转化成数组
            authorities = permissions.toArray(new String[0]);
        }

        xcUser.setPassword(null);
        String userJson= JSON.toJSONString(xcUser);
        UserDetails userDetails = User.withUsername(userJson).password(password).authorities(authorities).build();
        return  userDetails;
    }

}
